package com.zfzs.post.service.impl;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.dto.QueryTerAvgTransactionDTO;
import com.zfzs.post.model.vo.transaction.SumTranByLevelVO;
import com.zfzs.post.model.vo.transaction.SumTranByTypeVO;
import com.zfzs.post.model.vo.transaction.TerAvgTransactionVO;
import com.zfzs.post.model.vo.transaction.TerSumTransactionVO;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.modules.common.domain.AgentContact;
import com.zfzs.post.modules.common.service.AgentContactService;
import com.zfzs.post.modules.common.service.AgentTreeService;
import com.zfzs.post.modules.transaction.service.TransactionInfoService;
import com.zfzs.post.modules.transaction.service.dto.SumTranByLevelDTO;
import com.zfzs.post.modules.transaction.service.dto.SumTranByTypeDTO;
import com.zfzs.post.modules.transaction.service.dto.TerAvgTransactionDTO;
import com.zfzs.post.modules.transaction.service.dto.TerSumTransactionDTO;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.service.TransactionService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.DateStrUtils;
import com.zfzs.post.utils.MathUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author wq
 * @date 2019/10/16 16:49
 * @description
 */
@Service

public class TransactionServiceImpl implements TransactionService {


    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private JwtUserDetailsService jwtUserDetailsService;

    @Autowired
    private AgentTreeService agentTreeService;

    @Autowired
    private AgentContactService agentContactService;


    @Override
    public List<TerAvgTransactionVO> findTerAvgTransaction(QueryTerAvgTransactionDTO dto) {

        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();
        Map<String,Object> params=new HashMap<>(9);
        List<Long> teamList=agentContactService.findListByTeamType(dto.getTeamType(),agentInfo.getId(),agentInfo.getChannelId());

        if(StringUtils.isNotEmpty(dto.getTimeType())){
            params.putAll(DateStrUtils.getStartAndEndDate(dto.getTimeType()));
        }
        params.put("name",dto.getName());
        params.put("agentList",teamList);
        params.put("timeType",dto.getTimeType());
        params.put("orderType",dto.getOrderType());
        params.put("terminalTypeId",dto.getTerminalTypeId());
        List<TerAvgTransactionDTO> list=transactionInfoService.findTerAvgTransaction(params);
        List<TerAvgTransactionVO> result=new ArrayList<>();
        for (TerAvgTransactionDTO d : list) {
            TerAvgTransactionVO v=CloneUtils.clone(d,TerAvgTransactionVO.class);
            v.setAvgAmount(MathUtils.formatToNumber(d.getAvgAmount()));
            result.add(v);
        }

        return result;
    }

    @Override
    public List<TerSumTransactionVO> findTerSumTransaction(QueryTerAvgTransactionDTO dto) {
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();

        Map<String,Object> params=new HashMap<>(9);

        List<Long> teamList=agentContactService.findListByTeamType(dto.getTeamType(),agentInfo.getId(),agentInfo.getChannelId());

        if(StringUtils.isNotEmpty(dto.getTimeType())){
            params.putAll(DateStrUtils.getStartAndEndDate(dto.getTimeType()));
        }
        params.put("name",dto.getName());
        params.put("agentList",teamList);
        params.put("timeType",dto.getTimeType());
        params.put("orderType",dto.getOrderType());
        params.put("terminalTypeId",dto.getTerminalTypeId());
        List<TerSumTransactionDTO> list=transactionInfoService.findTerSumTransaction(params);
        List<TerSumTransactionVO> result=new ArrayList<>();
        for (TerSumTransactionDTO d : list) {
            TerSumTransactionVO v=CloneUtils.clone(d,TerSumTransactionVO.class);
            v.setSumAmount(MathUtils.formatToNumber(d.getSumAmount()));
            result.add(v);
        }

        return result;
    }

    @Override
    public Map<String, Object> findTranStatistics(Map<String, String> vo) {
        AgentDTO info = jwtUserDetailsService.getAgentInfo();
        if(StringUtils.isEmpty(vo.get("timeType"))){
            throw new BadRequestException("时间维度不能为空");
        }
        Map<String,Object> params=new HashMap<>();

        params.putAll(DateStrUtils.getStartAndEndDate(vo.get("timeType")));

        params.put("agentId",info.getId());

        Map<String,Object> result=new HashMap<>(2);

        List<SumTranByLevelDTO> levels = transactionInfoService.findSumTranGroupByLevel(params);

        completeLevel(levels);

        List<SumTranByLevelVO> vos = CloneUtils.batchClone(levels, SumTranByLevelVO.class);
        result.put("teamTypeTran",vos);

        List<SumTranByTypeDTO> types = transactionInfoService.findSumTranGroupByType(params);

        for (SumTranByTypeDTO type : types) {
            completeLevel(type.getDto());
        }

        result.put("terminalTypeTran",types);

        return result;
    }

    @Override
    public Map<String, Object> getTranSumStatistics(Map<String, String> params) {




        return null;
    }

    private void completeLevel(List<SumTranByLevelDTO> levels) {
        if(levels.size()!=3){
            for (int i = 0; i < 3; i++) {
                int finalI = i;
                boolean flag = levels.stream().noneMatch(e -> e.getLevel() == finalI);
                if(flag){
                    SumTranByLevelDTO dto=new SumTranByLevelDTO();

                    dto.setLevel(i);
                    dto.setAmount(new BigDecimal("0"));
                    levels.add(dto);
                }
            }
            levels.sort(Comparator.comparingInt(SumTranByLevelDTO::getLevel));
        }
    }
}
